﻿using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Unity;
using UnityEngine;
using UnityEngine.Networking;

public class Attack : CharacterBehaviour
{
    [CompilerGenerated]
    private static OnValueChangedEvent<Weapon> <>f__am$cache6;
    [CompilerGenerated]
    private static Action <>f__am$cache7;
    public bool isInfinteEnergy;
    private static int kCmdCmdAttack = -146478064;
    private static int kRpcRpcAttack;
    [SyncVar]
    private ushort m_energy = 0x3e8;
    [SyncVar]
    private ushort m_maxEnergy = 0x3e8;
    [SerializeField]
    private Weapon m_weapon;

    public event Action onAttack;

    public event OnValueChangedEvent<Weapon> onWeaponChanged;

    static Attack()
    {
        NetworkBehaviour.RegisterCommandDelegate(typeof(Attack), kCmdCmdAttack, new NetworkBehaviour.CmdDelegate(Attack.InvokeCmdCmdAttack));
        kRpcRpcAttack = 0x25e25766;
        NetworkBehaviour.RegisterRpcDelegate(typeof(Attack), kRpcRpcAttack, new NetworkBehaviour.CmdDelegate(Attack.InvokeRpcRpcAttack));
        NetworkCRC.RegisterBehaviour("Attack", 0);
    }

    public Attack()
    {
        if (<>f__am$cache6 == null)
        {
            <>f__am$cache6 = new OnValueChangedEvent<Weapon>(Attack.<onWeaponChanged>m__4D);
        }
        this.onWeaponChanged = <>f__am$cache6;
        if (<>f__am$cache7 == null)
        {
            <>f__am$cache7 = new Action(Attack.<onAttack>m__4E);
        }
        this.onAttack = <>f__am$cache7;
    }

    [CompilerGenerated]
    private static void <onAttack>m__4E()
    {
    }

    [CompilerGenerated]
    private static void <onWeaponChanged>m__4D(Weapon value, Weapon lastValue)
    {
    }

    protected override void Awake()
    {
        base.Awake();
    }

    public void CallCmdAttack(NetworkPosition position, NetworkDirection direction)
    {
        if (!NetworkClient.active)
        {
            Debug.LogError("Command function CmdAttack called on server.");
        }
        else if (base.isServer)
        {
            this.CmdAttack(position, direction);
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 5));
            writer.WritePackedUInt32((uint) kCmdCmdAttack);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            GeneratedNetworkCode._WriteNetworkPosition_None(writer, position);
            GeneratedNetworkCode._WriteNetworkDirection_None(writer, direction);
            base.SendCommandInternal(writer, 0, "CmdAttack");
        }
    }

    public void CallRpcAttack(NetworkPosition position, NetworkDirection direction)
    {
        if (!NetworkServer.active)
        {
            Debug.LogError("RPC Function RpcAttack called on client.");
        }
        else
        {
            NetworkWriter writer = new NetworkWriter();
            writer.Write((short) 0);
            writer.Write((short) ((ushort) 2));
            writer.WritePackedUInt32((uint) kRpcRpcAttack);
            writer.Write(base.GetComponent<NetworkIdentity>().netId);
            GeneratedNetworkCode._WriteNetworkPosition_None(writer, position);
            GeneratedNetworkCode._WriteNetworkDirection_None(writer, direction);
            this.SendRPCInternal(writer, 0, "RpcAttack");
        }
    }

    [Command]
    private void CmdAttack(NetworkPosition position, NetworkDirection direction)
    {
        this.CallRpcAttack(position, direction);
    }

    public virtual void DoAttack()
    {
        if (this.canAttack && base.hasAuthority)
        {
            if (base.isLocalPlayer)
            {
                this.DoAttackInternal();
            }
            this.CallCmdAttack(base.transform.position, base.transform.forward);
        }
    }

    internal void DoAttackInternal()
    {
        if (this.weapon != null)
        {
            this.weapon.user = base.self;
            this.weapon.Use();
            this.onAttack();
        }
    }

    public bool HasEnergy(float value)
    {
        return (!this.isInfinteEnergy ? (this.energy >= value) : true);
    }

    protected static void InvokeCmdCmdAttack(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkServer.active)
        {
            Debug.LogError("Command CmdAttack called on client.");
        }
        else
        {
            ((Attack) obj).CmdAttack(GeneratedNetworkCode._ReadNetworkPosition_None(reader), GeneratedNetworkCode._ReadNetworkDirection_None(reader));
        }
    }

    protected static void InvokeRpcRpcAttack(NetworkBehaviour obj, NetworkReader reader)
    {
        if (!NetworkClient.active)
        {
            Debug.LogError("RPC RpcAttack called on server.");
        }
        else
        {
            ((Attack) obj).RpcAttack(GeneratedNetworkCode._ReadNetworkPosition_None(reader), GeneratedNetworkCode._ReadNetworkDirection_None(reader));
        }
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        base.OnDeserialize(reader, initialState);
        if (initialState)
        {
            this.m_maxEnergy = (ushort) reader.ReadPackedUInt32();
            this.m_energy = (ushort) reader.ReadPackedUInt32();
        }
        else
        {
            int num = (int) reader.ReadPackedUInt32();
            if ((num & 1) != 0)
            {
                this.m_maxEnergy = (ushort) reader.ReadPackedUInt32();
            }
            if ((num & 2) != 0)
            {
                this.m_energy = (ushort) reader.ReadPackedUInt32();
            }
        }
    }

    protected virtual void OnDestroy()
    {
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        bool flag2 = base.OnSerialize(writer, forceAll);
        if (forceAll)
        {
            writer.WritePackedUInt32(this.m_maxEnergy);
            writer.WritePackedUInt32(this.m_energy);
            return true;
        }
        bool flag = false;
        if ((base.syncVarDirtyBits & 1) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.WritePackedUInt32(this.m_maxEnergy);
        }
        if ((base.syncVarDirtyBits & 2) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.WritePackedUInt32(this.m_energy);
        }
        if (!flag)
        {
            writer.WritePackedUInt32(base.syncVarDirtyBits);
        }
        return (flag | flag2);
    }

    [ClientRpc]
    private void RpcAttack(NetworkPosition position, NetworkDirection direction)
    {
        if (!base.isLocalPlayer)
        {
            base.transform.position = (Vector3) position;
            base.transform.rotation = Quaternion.LookRotation((Vector3) direction);
            this.DoAttackInternal();
        }
    }

    protected virtual void Start()
    {
    }

    private void UNetVersion()
    {
    }

    protected virtual void Update()
    {
    }

    public bool canAttack
    {
        get
        {
            return ((this.weapon != null) && this.weapon.canUse);
        }
    }

    public float energy
    {
        get
        {
            return (float) this.m_energy;
        }
        set
        {
            value = Mathf.Clamp(value, 0f, this.maxEnergy);
            if (!this.isInfinteEnergy)
            {
                this.Networkm_energy = (ushort) value;
            }
        }
    }

    public bool isAttacking
    {
        get
        {
            return ((this.weapon != null) && this.weapon.isCoolingdown);
        }
    }

    public float maxEnergy
    {
        get
        {
            return (float) this.m_maxEnergy;
        }
        set
        {
            this.Networkm_maxEnergy = (ushort) value;
        }
    }

    public ushort Networkm_energy
    {
        get
        {
            return this.m_energy;
        }
        [param: In]
        set
        {
            base.SetSyncVar<ushort>(value, ref this.m_energy, 2);
        }
    }

    public ushort Networkm_maxEnergy
    {
        get
        {
            return this.m_maxEnergy;
        }
        [param: In]
        set
        {
            base.SetSyncVar<ushort>(value, ref this.m_maxEnergy, 1);
        }
    }

    public Weapon weapon
    {
        get
        {
            return this.m_weapon;
        }
        set
        {
            ValueUtility.ChangeValue<Weapon>(ref this.m_weapon, value, this.onWeaponChanged);
        }
    }
}

